package my.example.samplegridview;

import java.io.BufferedInputStream;
import java.text.DecimalFormat;
import java.util.Vector;

import my.example.samplegridview.modal.product;
import o2s.utils.imageHelper;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OrderPreviewAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private Vector<product> products;

    public OrderPreviewAdapter(Context c , Vector<product> products) {
        mContext = c;            
        this.products = products;
    }

    @Override
	public int getCount() {
        return this.products.size();
    }

    @Override
	public Object getItem(int position) {
        return position;
    }

    @Override
	public long getItemId(int position) {
        return position;
    }

    @Override
	public View getView(int position, View convertView, ViewGroup parent) {
    	
    	final product p = products.get(position);
    	
    	LinearLayout productView = null;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
        	LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	productView = (LinearLayout) li.inflate(R.layout.orderpreviewitem, null);
        } else {
        	productView = (LinearLayout) convertView;
        }
        
        BufferedInputStream buf = null;
        
        //Set Image
        ImageView iv = (ImageView)productView.findViewById(R.id.orderpreviewitemimage);
        if(p.getImage().length() > 0){
			int indexer = p.getImage().lastIndexOf("/");
			String imageName = p.getImage().substring(indexer+1);
			Bitmap bitmap = imageHelper.readImageFromUrl(imageName, mContext);
			iv.setImageBitmap(bitmap);
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
		}
        
        TextView tv = (TextView) productView.findViewById(R.id.orderpreviewitemqty);
        tv.setText(p.getOrderedQuantity());
        
        tv = (TextView) productView.findViewById(R.id.orderpreviewitemremarks);
        tv.setText(p.getRemarks());
        
        // Set Extra Charge
		double extracharge = 0;
		String extraname = "";
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		double orderedQuantity = Double.parseDouble(p.getOrderedQuantity());
		for(int i1=0;i1<p.getProduct_options(true).size();i1++){
			int selectoption = p.getProduct_options(true).get(i1).getSelectedoption();
			extracharge += orderedQuantity * Double.valueOf(p.getProduct_options(true).get(i1).getProduct_option_values(true).get(selectoption).getPrice());
			extraname += p.getProduct_options(true).get(i1).getProduct_option_values(true).get(selectoption).getProoptionvaluedescription().getName() + " ";
		}
		tv = (TextView) productView.findViewById(R.id.orderpreviewitemoptions);
		tv.setText(String.valueOf(extracharge));
        
        tv = (TextView) productView.findViewById(R.id.orderpreviewitemprice);
		tv.setText(p.getPrice());

		tv = (TextView) productView.findViewById(R.id.orderpreviewitemsubtotal);
		double pricePerUnit = Double.parseDouble(p.getPrice());
		double price = Double.valueOf(twoDForm.format((orderedQuantity*pricePerUnit)));
		tv.setText("RM " + String.valueOf(price));
		
		// Set name
		tv = (TextView) productView
				.findViewById(R.id.orderpreviewitemname);
		if(extraname.length() > 0){
			extraname = " ( " + extraname + " ) " ;
		}
		tv.setText(p.getProdescription().getName() + extraname);
		
		// Set status
		if(p.getProductStatus()!=null){
			tv = (TextView) productView
					.findViewById(R.id.orderpreviewitemstatus);
			tv.setText(p.getProductStatus());
			
			iv = (ImageView)productView.findViewById(R.id.orderpreviewplacedimage);
			if(p.getProductStatus().compareTo("Placed") == 0){
				iv.setVisibility(View.VISIBLE);
			}
			else{
				iv.setVisibility(View.GONE);
			}
		}
		
		return productView;
    }
}    